package com.jci.vsd.network.control;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jci.vsd.bean.MessageBean;
import com.jci.vsd.bean.notice.NoticeRequestBean;
import com.jci.vsd.bean.notice.UnreadNoticeBean;
import com.jci.vsd.constant.AppConstant;
import com.jci.vsd.exception.IApiException;
import com.jci.vsd.network.api.NoticeCenterApi;
import com.jci.vsd.utils.Loger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import okhttp3.Headers;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by liqing on 17/12/6.
 */

public class NoticeCenterControl extends BaseControl {

    //xiaoxi


    public Observable<List<MessageBean>> getMessages() {
        Retrofit retrofit = builderRetrofitWithHeader();
        NoticeCenterApi api = retrofit.create(NoticeCenterApi.class);
        Observable<Response<String>> observable = api.getMessages();

        return observable.map(new Function<Response<String>, List<MessageBean>>() {
            @Override
            public List<MessageBean> apply(Response<String> s) throws Exception {
                Loger.e("message--s=" + s);
                Headers headers = s.headers();
                String authStr = headers.get("Authorization");
                Loger.e("header-authStr" + authStr);
                int codeHttp = s.code();
                if (codeHttp == 401) {
                    throw new IApiException("401", "401");
                }

                /*
                  JSONObject jsonObject = JSON.parseObject(stringResponse.body());
                if (jsonObject.getIntValue(AppConstant.JSON_CODE) == 200) {
                    JSONObject dataObj = jsonObject.getJSONObject(AppConstant.JSON_DATA);
                    List<BudgetBean> beanList = JSON.parseArray(dataObj.getString("vos"), BudgetBean.class);
                    return beanList;
                }
                 */

                JSONObject jsonObject = JSON.parseObject(s.body());
                if (jsonObject.getIntValue(AppConstant.JSON_CODE) == 200) {

                    JSONObject dataObj = jsonObject.getJSONObject(AppConstant.JSON_DATA);

//                    List<HomeAuthorityBean> authorityBeanList = JSON.parseArray(dataObj.getString("Authority"), HomeAuthorityBean.class);
//                    loginResponseBean.setList(authorityBeanList);
                    List<MessageBean> beanList = JSON.parseArray(dataObj.getString("msgs"), MessageBean.class);
                    return beanList;
                }
                throw new IApiException("messages", jsonObject.getString(AppConstant.JSON_MESSAGE));
            }
        });

    }

    public Observable<List<MessageBean>> geUnReadtMessages() {
        Retrofit retrofit = builderRetrofitWithHeader();
        NoticeCenterApi api = retrofit.create(NoticeCenterApi.class);
        Observable<Response<String>> observable = api.getUnReadMessages();

        return observable.map(new Function<Response<String>, List<MessageBean>>() {
            @Override
            public List<MessageBean> apply(Response<String> s) throws Exception {
                Loger.e("message--s=" + s);
                Headers headers = s.headers();
                String authStr = headers.get("Authorization");
                String httpMsg = s.message();
                Loger.e("header-authStr" + authStr + "httpMsg--" + httpMsg);

                int codeHttp = s.code();
              //  codeHttp = 401;
//                if (codeHttp == 401) {
//                    throw new IApiException("401", "401");
//                }

                /*
                  JSONObject jsonObject = JSON.parseObject(stringResponse.body());
                if (jsonObject.getIntValue(AppConstant.JSON_CODE) == 200) {
                    JSONObject dataObj = jsonObject.getJSONObject(AppConstant.JSON_DATA);
                    List<BudgetBean> beanList = JSON.parseArray(dataObj.getString("vos"), BudgetBean.class);
                    return beanList;
                }
                 */

                JSONObject jsonObject = JSON.parseObject(s.body());
                if (codeHttp == 200) {
                    if (jsonObject.getIntValue(AppConstant.JSON_CODE) == 200) {

                        JSONObject dataObj = jsonObject.getJSONObject(AppConstant.JSON_DATA);

//                    List<HomeAuthorityBean> authorityBeanList = JSON.parseArray(dataObj.getString("Authority"), HomeAuthorityBean.class);
//                    loginResponseBean.setList(authorityBeanList);
                        List<MessageBean> beanList = JSON.parseArray(dataObj.getString("msgs"), MessageBean.class);
                        return beanList;
                    }

                    throw new IApiException("messages", jsonObject.getString(AppConstant.JSON_MESSAGE));
                } else if (codeHttp == 401) {
                    List<MessageBean> beanList = new ArrayList<>();
                    MessageBean messageBean = new MessageBean();
                    messageBean.setType(0);
                    beanList.add(messageBean);
                    return beanList;
                }
                throw new IApiException("messages", httpMsg);

            }
        });

    }


    //设置 信息已读


    public Observable<Boolean> setMsgReaded(int id) {
        Retrofit retrofit = builderJsonRetrofit();
        Map<String, Object> paramMap = new HashMap<>();

        paramMap.put("id", id);
        String paramStr = JSON.toJSONString(paramMap);
        Loger.e("--setMsgReadedParamStr" + paramStr);
        NoticeCenterApi api = retrofit.create(NoticeCenterApi.class);
        Observable<Response<String>> observable = api.setMsgReaded(paramStr);
        return observable.map(new Function<Response<String>, Boolean>() {
            @Override
            public Boolean apply(Response<String> stringResponse) throws Exception {
                String responseStr = stringResponse.body();
                int codeHttp = stringResponse.code();
                if (codeHttp == 401) {
                    throw new IApiException("401", "401");
                }

                JSONObject jsonObject = JSON.parseObject(responseStr);
                int code = jsonObject.getIntValue(AppConstant.JSON_CODE);
                if (code == 200) {
                    return true;
                }
                throw new IApiException("消息设置已读", jsonObject.getString(AppConstant.JSON_MESSAGE));


            }


        });
    }

    //删除消息
    public Observable<Boolean> deleteMsg(int id) {
        Retrofit retrofit = builderJsonRetrofit();
        Map<String, Object> paramMap = new HashMap<>();

        paramMap.put("id", id);
        String paramStr = JSON.toJSONString(paramMap);
        Loger.e("--setMsgReadedParamStr" + paramStr);
        NoticeCenterApi api = retrofit.create(NoticeCenterApi.class);
        Observable<Response<String>> observable = api.deleteMsg(paramStr);
        return observable.map(new Function<Response<String>, Boolean>() {
            @Override
            public Boolean apply(Response<String> stringResponse) throws Exception {
                String responseStr = stringResponse.body();
                int codeHttp = stringResponse.code();
                if (codeHttp == 401) {
                    throw new IApiException("401", "401");
                }

                JSONObject jsonObject = JSON.parseObject(responseStr);
                int code = jsonObject.getIntValue(AppConstant.JSON_CODE);
                if (code == 200) {
                    return true;
                }
                throw new IApiException("消息删除", jsonObject.getString(AppConstant.JSON_MESSAGE));


            }


        });
    }

    public Observable<List<UnreadNoticeBean>> getNotice(NoticeRequestBean requestBean) {
        Retrofit retrofit = builderRetrofit();
        Map<String, String> parmsMap = new HashMap<>();
        parmsMap.put("Receiver", requestBean.getReceiver());
        if (!TextUtils.isEmpty(requestBean.getStatus())) {
            parmsMap.put("Status", requestBean.getStatus());
        }
        parmsMap.put("PageSize", requestBean.getPageSize());
        parmsMap.put("PageIndex", requestBean.getPageIndex());
        NoticeCenterApi api = retrofit.create(NoticeCenterApi.class);
        Observable<String> observable = api.getNotice(parmsMap);
        Loger.i(" parmsMap= " + parmsMap);
        return observable.map(new Function<String, List<UnreadNoticeBean>>() {
            @Override
            public List<UnreadNoticeBean> apply(String s) throws Exception {
                Loger.e("notice--s=" + s);
                JSONObject jsonObject = JSON.parseObject(s);
                if (!"500".equals(jsonObject.getString(AppConstant.JSON_STATUS))) {
                    List<UnreadNoticeBean> beanList = JSON.parseArray(jsonObject.getString(AppConstant.JSON_DATA), UnreadNoticeBean.class);
                    return beanList;
                }
                throw new IApiException("notice", jsonObject.getString(AppConstant.JSON_MESSAGE));
            }
        });

    }

    /**
     * 到料提醒
     */
    public Observable<List<UnreadNoticeBean>> getMessage(NoticeRequestBean requestBean) {
        Retrofit retrofit = builderRetrofit();
        Map<String, String> parmsMap = new HashMap<>();
        parmsMap.put("Receiver", requestBean.getReceiver());
        if (!TextUtils.isEmpty(requestBean.getStatus())) {
            parmsMap.put("Status", requestBean.getStatus());
        }
        parmsMap.put("PageSize", requestBean.getPageSize());
        parmsMap.put("PageIndex", requestBean.getPageIndex());
        parmsMap.put("msgType", "msg");
        NoticeCenterApi api = retrofit.create(NoticeCenterApi.class);
        Observable<String> observable = api.getNotice(parmsMap);
        Loger.i(" parmsMap= " + parmsMap);
        return observable.map(new Function<String, List<UnreadNoticeBean>>() {
            @Override
            public List<UnreadNoticeBean> apply(String s) throws Exception {
                Loger.e("notice--s=" + s);
                JSONObject jsonObject = JSON.parseObject(s);
                if (!"500".equals(jsonObject.getString(AppConstant.JSON_STATUS))) {
                    List<UnreadNoticeBean> beanList = JSON.parseArray(jsonObject.getString(AppConstant.JSON_DATA), UnreadNoticeBean.class);
                    return beanList;
                }
                throw new IApiException("notice", jsonObject.getString(AppConstant.JSON_MESSAGE));
            }
        });

    }


    //setRead
    public Observable<Boolean> setNoticeRead(String id) {
        Retrofit retrofit = builderRetrofit();
        Map<String, String> parmsMap = new HashMap<>();
        parmsMap.put("msgid", id);
        NoticeCenterApi api = retrofit.create(NoticeCenterApi.class);
        Observable<String> observable = api.setNoticeRead(parmsMap);
        return observable.map(new Function<String, Boolean>() {
            @Override
            public Boolean apply(@NonNull String s) throws Exception {
                JSONObject jsonObject = JSON.parseObject(s);
                if ("200".equals(jsonObject.getString(AppConstant.JSON_STATUS))) {
                    return true;
                }
                throw new IApiException("read", jsonObject.getString(AppConstant.JSON_MESSAGE));
            }
        });
    }


// 消息推送的获取

    public Observable<List<UnreadNoticeBean>> getPushNotice(NoticeRequestBean requestBean) {
        Retrofit retrofit = builderRetrofit();
        Map<String, String> parmsMap = new HashMap<>();
        parmsMap.put("Receiver", requestBean.getReceiver());
        NoticeCenterApi api = retrofit.create(NoticeCenterApi.class);
        Observable<String> observable = api.getPushNotice(parmsMap);
        Loger.i(" parmsMap= " + parmsMap);
        return observable.map(new Function<String, List<UnreadNoticeBean>>() {
            @Override
            public List<UnreadNoticeBean> apply(String s) throws Exception {
                Loger.e("pushnotice--s=" + s);
                JSONObject jsonObject = JSON.parseObject(s);
                if (!"500".equals(jsonObject.getString(AppConstant.JSON_STATUS))) {
                    List<UnreadNoticeBean> beanList = JSON.parseArray(jsonObject.getString(AppConstant.JSON_DATA), UnreadNoticeBean.class);
                    return beanList;
                }
                throw new IApiException("pushnotice", jsonObject.getString(AppConstant.JSON_MESSAGE));
            }
        });

    }

    /**
     * 到料提醒未读消息
     *
     * @param requestBean
     * @return
     */
    public Observable<List<UnreadNoticeBean>> getUnreadMsg(NoticeRequestBean requestBean) {
        Retrofit retrofit = builderRetrofit();
        Map<String, String> parmsMap = new HashMap<>();
        parmsMap.put("Receiver", requestBean.getReceiver());
        parmsMap.put("msgType", "msg");
        NoticeCenterApi api = retrofit.create(NoticeCenterApi.class);
        Observable<String> observable = api.getPushNotice(parmsMap);
        Loger.i(" parmsMap= " + parmsMap);
        return observable.map(new Function<String, List<UnreadNoticeBean>>() {
            @Override
            public List<UnreadNoticeBean> apply(String s) throws Exception {
                Loger.e("pushnotice--s=" + s);
                JSONObject jsonObject = JSON.parseObject(s);
                if (!"500".equals(jsonObject.getString(AppConstant.JSON_STATUS))) {
                    List<UnreadNoticeBean> beanList = JSON.parseArray(jsonObject.getString(AppConstant.JSON_DATA), UnreadNoticeBean.class);
                    return beanList;
                }
                throw new IApiException("pushnotice", jsonObject.getString(AppConstant.JSON_MESSAGE));
            }
        });

    }
}
